package com.liull.netty.nio.future;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.EventExecutor;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.ExecutionException;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 21:12 2021/7/24
 * @Modified By:
 */
@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoopGroup groups = new NioEventLoopGroup(2);
        // 结果容器 其他线程可以将自己算的结果放进去
        DefaultPromise<Integer> promise = new DefaultPromise(groups.next());

        // 线程1
        new Thread(() -> {
            // 计算结果
            log.debug("正在计算结果");
            final int i = new Random().nextInt(10);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            promise.setSuccess(i);
        }).start();
        log.debug("执行计算");
        final Integer res = promise.get();
        log.debug("等待执行结果：{}", res);

    }
}
